• File: gps_tracker_get_history.php
  • Full Path: C:/htdocs/reeft_gps_test/gps_tracker_get_history.php
  • Date Modified: 04/09/2025 9:22 AM
  • File size: 3.92 KB
  • MIME-type: text/x-php
  • Charset: utf-8
<?php
//======================================================================================
//
// Function: Get history/route for a device
//
// Programmer: AR
// Date      : 2024-11-29
//
// Copyright Reeft A/S (c) - 2024
//======================================================================================

// https://login.gps-tracker.dk/api/get_history?user_api_hash=$2y$10$SC.XMMJovzAiHijfPf/0F.AAatv6EBPw.4S0DbyQCUHNErB7qfd4q&device_id=1438&from_date=2024-11-28&to_date=2024-11-29&from_time=00:00&to_time=00:00

//======================================================================================
// General 
//======================================================================================
include "include/apikey.php";

//======================================================================================
// Get input
//======================================================================================

if (isset($_REQUEST["from_date"]))    $from_date = $_REQUEST["from_date"];
else $from_date = 0;

if (isset($_REQUEST["to_date"]))   	   $to_date = $_REQUEST["to_date"];
else $to_date =0;

if (isset($_REQUEST["device_id"]))    $device_id = $_REQUEST["device_id"];
else $device_id =0;

$next_date = strtotime("1 day", strtotime($to_date));
$to_date_next = date("Y-m-d", $next_date);


$from_time = '00:00';
$to_time = '00:00';

$parms 	= '?from_date=' . $from_date
		. '&from_time=' . $from_time
		. '&to_date=' . $to_date_next
		. '&to_time=' . $to_time
		. '&device_id=' . $device_id
		. '&user_api_hash=' . $gsmKey
		;

// Set URL
$url = $gsmUrl . '/' . 'get_history';	

// Add parms
$url = $url . $parms;

//	echo $url;

// Create a new cURL resource
$ch = curl_init($url);

// Set the content type to application/json
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));	

// Return response instead of outputting
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

curl_setopt($ch, CURLOPT_POST, false);
curl_setopt($ch, CURLOPT_HEADER, false);    // we do not need headers
curl_setopt($ch, CURLOPT_NOBODY, false);    // we don't need body

// Execute the GET request
$result = curl_exec($ch);

$error_msg = "";
if (curl_errno($ch)) {
	$error_msg = curl_error($ch);
}

// Close cURL resource
curl_close($ch);

$data = json_decode($result, true);

$returnResult = [];

$returnResult["error"]				= $error_msg;
$returnResult["events"]				= array();

if ($error_msg == "") {
	
	$returnResult["data"]			= array();
	
	$count = 0;
	$eventType = "";
	$startLat = "";
    $startLng = "";
    $endLat = "";
    $endLng = "";
	$startTime = "";
    $endTime = "";
    foreach ($data['items'] as $item) {
		
		$event = array( "id"			=> $count,
						"status"		=> $item['status'],
						"time" 			=> @$item['time'],
						"start_time" 	=> $item['show'],
						"stop_time"  	=> @$item['left'],
						"raw_time" 		=> $item['raw_time'],
						"time_seconds"  => @$item['time_seconds'],
						"distance"  	=> @$item['distance']
						);
		$returnResult["events"][] = $event;
		
        // if data has items
        if (isset($item['items']) && count($item['items']) > 0) {
			
			$rowCount = count($item['items']);

			for ($i = 0; $i < $rowCount; $i++) {
				$currentPoint = $item['items'][$i];
				
				$timestamp = strtotime($currentPoint['time']);
				$trans = array( "event_id"	=> $count,
								"item_id"	=> $currentPoint['item_id'],
								"time"		=> $currentPoint['time'],
								"timestamp" => $timestamp,
								"device_id" => $currentPoint['device_id'],
								"latitude"  => $currentPoint['lat'],
								"longitude" => $currentPoint['lng'],
								"course"  	=> $currentPoint['course'],
								"speed"  	=> $currentPoint['speed'],
								"distance"  => $currentPoint['distance']
								);				
				
				$returnResult["data"][] = $trans;

			}
        }
		$count++;
    }
}	

echo json_encode($returnResult);